<?php
declare (strict_types=1);

namespace app\service;

use app\controller\admin\MatchParam;
use app\model\CategoryColorParam;
use app\traits\ServiceTrait;
use think\facade\Db;

/**
 * CategoryColorParam
 * Class CategoryColorParamService
 * @package app\service
 */
class CategoryColorParamService
{
    //模型，带命名空间
    public static $model = 'app\model\CategoryColorParam';
    //模型主键
    public static $pk = 'id';

    use ServiceTrait;

    /**
     * @param $labelId
     * @return array
     */
    public static function queryBylabel($labelId)
    {
        // 列表
        $table = CategoryColorParam::$_table;
        $r = Db::table($table)->where(['label_id' => $labelId])->select();
        if (empty($r)) {
            return [];
        }

        $groupData = [];
        foreach ($r as $v) {
            $groupData[$v['category_id']][] = [
                'categoryId' => $v['category_id'],
                'colorGroupId' => $v['color_group_id'],
                'createTime' => '',
                'id' => $v['id'],
                'labelId' => $v['label_id'],
                'paramVal' => $v['param_val'],
                'remark' => '',
                'updateTime' => '',
            ];
        }
        return $groupData;
    }

    public static function addBatch($data)
    {
        $addData = [];
        if (empty($data)) {
            return [];
        }
        $labelId = $data[0]['labelId'];
        foreach ($data as $v) {
            $addData[] = [
                'label_id' => $v['labelId'],
                'category_id' => $v['categoryId'],
                'color_group_id' => $v['colorGroupId'],
                'param_val' => $v['paramVal'],
                'remark' => '',
            ];
        }
        CategoryColorParam::where(['label_id' => $labelId])->delete();
        CategoryColorParam::insertAll($addData);
    }
}
